package com.isunland.managesystem.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.isunland.managesystem.R;
import com.isunland.managesystem.entity.Message;

public class MessageReceiveListAdapter extends ArrayAdapter<Message> {

  /**
   * ViewHolder class for layout.<br />
   * <br />
   * Auto-created on 2015-07-15 11:12:26 by Android Layout Finder
   * (http://www.buzzingandroid.com/tools/android-layout-finder)
   */
  private static class ViewHolder {
    public final RelativeLayout rootView;
    public final TextView tvLvReceivepeople;
    public final TextView tvLvSendtime;
    public final TextView tvIsRead;
    public final TextView tvLvMessagecontent;

    private ViewHolder(RelativeLayout view, TextView tvLvReceivepeople, TextView tvLvSendtime,
        TextView tvLvMessagecontent, TextView tvIsRead) {
      this.rootView = view;
      this.tvLvReceivepeople = tvLvReceivepeople;
      this.tvLvSendtime = tvLvSendtime;

      this.tvLvMessagecontent = tvLvMessagecontent;
      this.tvIsRead = tvIsRead;
    }

    public static ViewHolder create(RelativeLayout view) {
      TextView tvLvReceivepeople = (TextView) view.findViewById(R.id.tv_lv_receivepeople);
      TextView tvLvSendtime = (TextView) view.findViewById(R.id.tv_lv_sendtime);

      TextView tvLvMessagecontent = (TextView) view.findViewById(R.id.tv_lv_messagecontent);
      TextView tvIsRead = (TextView) view.findViewById(R.id.tv_isRead_messageReceive);
      return new ViewHolder(view, tvLvReceivepeople, tvLvSendtime, tvLvMessagecontent, tvIsRead);
    }
  }

  @Override public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder vh;
    if (convertView == null) {
      View view = mInflater.inflate(R.layout.adapter_message_receive, parent, false);
      vh = ViewHolder.create((RelativeLayout) view);
      view.setTag(vh);
    } else {
      vh = (ViewHolder) convertView.getTag();
    }

    Message item = getItem(position);
    vh.tvLvReceivepeople.setText(mContext.getString(R.string.sendPeople, item.getUserName()));
    vh.tvLvSendtime.setText(mContext.getString(R.string.sendtime, item.getSendTime()));

    vh.tvLvMessagecontent.setText(mContext.getString(R.string.messageContent, item.getContent()));
    String ifRead = item.getIfRead();

    if (ifRead != null && ifRead.equalsIgnoreCase("F")) {
      vh.tvIsRead.setVisibility(View.VISIBLE);
      vh.tvIsRead.setText(R.string.hasNotRead);
      vh.tvIsRead.setTextColor(mContext.getResources().getColor(R.color.white));
      vh.tvIsRead.setBackgroundColor(mContext.getResources().getColor(R.color.red));
    } else {
      vh.tvIsRead.setVisibility(View.GONE);
    }

    return vh.rootView;
  }

  private LayoutInflater mInflater;
  private Context mContext;

  // Constructors
  public MessageReceiveListAdapter(Context context, List<Message> objects) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
    this.mContext = context;
  }

  private MessageReceiveListAdapter(Context context, Message[] objects) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
    this.mContext = context;
  }
}
